package com.stars.home.activity

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.view.View
import com.hwangjr.rxbus.RxBus
import com.qmuiteam.qmui.kotlin.onClick
import com.stars.home.adapter.BankListAdapter
import com.stars.home.base.MBRActivity
import com.stars.home.config.ConfigRx
import com.stars.home.config.MIntentAction
import com.stars.home.databinding.ActivityBankListBinding
import com.stars.home.model.BackList
import com.stars.home.model.bean.ChoseBankBean
import com.stars.home.vmodel.BankListVModel

/**
 * 银行卡列表
 *on 2022/10/4
 */
class BankListActivity: MBRActivity<BankListVModel, BankListAdapter, ActivityBankListBinding>() {
    var type:String?=""
    var id:String?=null
    var card:String?=null
    var cardName:String?=null
    override fun init(savedInstanceState: Bundle?) {
        super.init(savedInstanceState)
         type=intent.extras?.getString("type","")
        if(type=="1"){
            titleBar()?.tvRight?.text="确定"
            titleBar()?.tvRight?.visibility=View.VISIBLE
        }

    }

    override fun listeners() {
        super.listeners()

        adapter().setOnItemClickListener { adapter, view, position ->

            var list=adapter.data as List<BackList>
            for((index,item)in list.withIndex()){
                if(index==position){
                    id=item.id
                    cardName=item.name
                    card=item.card
                    item.chose=true
                    adapter.notifyItemChanged(position)
                }else{
                    if(item.chose){
                        item.chose=false
                        adapter.notifyItemChanged(index)
                    }
                }
            }


        }

        titleBar()?.tvRight?.onClick {
            if(id.isNullOrEmpty()) {
                toast("请选择银行卡")
                return@onClick
              }

                var bean= ChoseBankBean(id,cardName,card)
                RxBus.get().post(ConfigRx.CHOSE_BANK, bean)
                finish()


            }

    }
    override fun vmClass(): Class<BankListVModel> {
        return BankListVModel::class.java
    }
    override fun binding(): ActivityBankListBinding {
      return ActivityBankListBinding.inflate(layoutInflater)
    }

    override fun initAdapter(): BankListAdapter {
       return BankListAdapter(vm)
    }

    override fun title(): String? {
        return "选择银行卡"
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode != Activity.RESULT_OK) {
            return
        }
        if (requestCode == MIntentAction.REQUEST_CODE_TWO) {
            //添加地址 --刷新
            onRefresh()

            return
        }
    }
}
